package com.sdi.columbus;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.widget.FrameLayout;
import android.widget.TableLayout;
import android.widget.TextView;

public class DisplayReport extends Activity {

	
	TextView t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t;
	Intent news;
	Bundle bun;
//	int VehicleId;
	String VehicleId;
	GetReport get;
	String stime,etime,accountID;
	String responses,reports;
	int MaxSpeed,MinSpeed;
	int AvgSpeed;
	int MaxAltitude,MinAltitude;
	int AvgAltitude;
	String runningtime,duration;
	int distance;
	int regindex;
	String avgalt,avgspd,stopt,maxalt,minalt;
	private static Double green;
	private static String regno;
	GpsData gpss=new GpsData();
	String[] VehicleRegNo=new String[gpss.getSize()];
//	int [] VehicleIds=new int[gpss.getSize()];
	String [] VehicleIds=new String[gpss.getSize()];
	private static int value;
	public int getValue()
	{
		return value;
	}
	public double getSpeed1() {
	    return green;
	}
	
	
	FrameLayout frames;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.report_display);
		
		news=getIntent();
		bun=news.getExtras();
//		VehicleId=bun.getInt("vehicleids");
		VehicleId=bun.getString("vehicleids");
		stime=bun.getString("start");
		etime=bun.getString("end");
		
		Display display = getWindowManager().getDefaultDisplay(); 
		int width = display.getWidth();
		int height = display.getHeight();
      
        value=width;
        System.out.println("screen size"+value);
        VehicleRegNo=gpss.getVehicleRegNo();
//		VehicleIds=gpss.getVehicleIds();
		VehicleIds=gpss.getVehicleIds();
		accountID=ColombusApp.getSharedApp().accountId;
		
		for(int q=0;q<gpss.getSize();q++)
		{
			if(VehicleIds[q]==VehicleId)
			{
				regindex=q;
			}
		}
		regno=VehicleRegNo[regindex];
		t=(TextView)findViewById(R.id.textView1);
		t1=(TextView)findViewById(R.id.tv3);
		t2=(TextView)findViewById(R.id.tv6);
		t3=(TextView)findViewById(R.id.tv9);
		t4=(TextView)findViewById(R.id.tv12);
		t5=(TextView)findViewById(R.id.tv15);
		t6=(TextView)findViewById(R.id.tv18);
		t7=(TextView)findViewById(R.id.tv21);
		t8=(TextView)findViewById(R.id.tv24);
		t9=(TextView)findViewById(R.id.tv27);
		t10=(TextView)findViewById(R.id.tv30);
		
		System.out.println(" Selected Vehicle ID from display report is : "+VehicleId);
		System.out.println(" start time from display report is : "+stime);
		System.out.println(" end time from display report is : "+etime);
		
        
		frames=(FrameLayout)findViewById(R.id.frlt);
	    TableLayout tableLayout=(TableLayout)findViewById(R.id.tbll);
		
		get=new GetReport();
    	try {
			responses=get.GetValues(accountID,VehicleId,stime,etime);
			JSONObject json = new JSONObject(responses);
			reports = json.getString("GetTripSummaryResult");
			System.out.println("first report"+reports);
			
			JSONObject json1 = new JSONObject(reports);
			
			
			MaxSpeed=Integer.parseInt(json1.getString("MaximumSpeed"));
			MinSpeed=Integer.parseInt(json1.getString("MinimumSpeed"));
			runningtime=json1.getString("TotalRunningTime");
			distance=Integer.parseInt(json1.getString("TotalDistance"));
			green=Double.parseDouble(json1.getString("GreenDriveIndex"));
			duration=json1.getString("TotalDuration");
			avgalt=json1.getString("AverageAltitude");
			avgspd=json1.getString("AverageSpeed");
			stopt=json1.getString("TotalStoppageTime");
			minalt=json1.getString("MinimumAltitude");
			maxalt=json1.getString("MaximumAltitude");
			
			System.out.println("aVerage"+avgalt);
			System.out.println("stoptime"+stopt);
			System.out.println("avgspd"+avgspd);
			System.out.println("min alt"+minalt);
			System.out.println("max alt"+maxalt);
			
	        Intent achartIntent = new Chart().execute(DisplayReport.this,frames);
	        	    
	        	    
	       	 t.setText(regno);                                             
	         t1.setText(String.valueOf(MaxSpeed)+" Km/hr"); 
	         t2.setText(maxalt+" M"); 
	         t3.setText(String.valueOf(MinSpeed)+" Km/hr"); 
	         t4.setText(minalt+" M"); 
	         t5.setText(avgspd+" Km/hr"); 
	         t6.setText(avgalt+" M"); 
	         t7.setText(runningtime); 
	         t8.setText(stopt); 
	         t9.setText(String.valueOf(distance)+" Km"); 
	         t10.setText(duration); 
	       	        
			System.out.println("max sped"+MaxSpeed);
			System.out.println("min sped"+MinSpeed);
			System.out.println("run time"+runningtime);
			System.out.println("dist"+distance);
			System.out.println("green"+green);
			
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
    				
	}

	
}
